package cn.tude.cn.tedu;

import sun.misc.IOUtils;

import java.io.*;
import java.util.Scanner;

public class Test_IOPravate {
    public static void main(String[] args) {

        System.out.println("请输入源文件路径:");
        String from = new Scanner(System.in).nextLine();
        System.out.println("请输入目标文件路径:");
        String to = new Scanner(System.in).nextLine();
        copy(from, to);
        System.out.println("文件复制完成");
    }



    public static void copy(String from, String to) {
        InputStream in = null;
        OutputStream out = null;
        try {
            //创建读写流对象
            in = new BufferedInputStream(new FileInputStream(from));
            out = new BufferedOutputStream(new FileOutputStream(to));
            //2.边读边写
            int b;//定义变量,记录read()读到的数据
            while ((b = in.read()) != -1) {
                out.write(b);
            }
        } catch (IOException e) {
            System.out.println("文件复制失败");
        } finally {
            IOUtils.close(in);
            IOUtils.close(out);
        }

    }


    public static class IOUtils {
        public static void close(Closeable in) {
            if (in != null) {
                try {
                    in.close();

                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}






